<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
use App\Org\Image;

class ConfigController extends Controller
{
    
    //网站配置首页
    public function index()
    {	
    	$list = DB::table('storeConfig')->get();

    	// return  111;
    	
    	return view('admin.storeconfig.table',['list' => $list]);
    }

    //更新配置页面
    public function edit($id)
    {	
    	$list = DB::table('storeConfig')->first();
    	
    	return view('admin.storeconfig.edit',['list'=>$list]);
    }

    //执行更新
    public function update($id,Request $request){

    	//自定义错误信息
    	$message = [
    		'required' => ':attribute 是必须的',
    	];

    	//设定验证规则
    	$this -> validate($request,[
    		'configTitle' => 'required',
    		'configKeys' => 'required',
    		'configDesc' => 'required',
    		'configLogo' => 'required',
    	],$message);

    	//如果有文件获取文件信息
    	if ( $request->hasFile('configLogo') ) {

    		$file = $request->file('configLogo');

    	// 文件在上传过程中是否出错
    		if ( $file->isValid() ) {

    			//获取后缀名
    			$type = $file->getClientOriginalExtension();
    			//新的logo 名
    			 $logoName = time().rand(1000,99999).'.'.$type;

    			 $file -> move('./uploads/',$logoName);

    			 if ( $file -> getError() > 0 ) {
    			 	
    			 		echo '上传失败'; 

    			 } else {
    			 	
    			  $data = $request->except('_token','_method');
    			 
    			  $data['configLogo'] = $logoName;

    			  
    			  $row = DB::table('storeConfig') -> where('configId',$id) -> update($data);

    			  if ( $row >0 ) {

    			  		return redirect('/admin/storeConfig')->with("msg",'修改成功');
    			  }
    			 }
    		}
    	}
    	
  //   	//使用自定义类进行等比缩放
  //   	$img = new Image();

  //   	$img->open('uploads/'.$logoName);
		// $img->thumb(240,114)->save('./uploads/s_'.$logoName);
		}
}
